
class Observer {
    constructor(data) {
       this.walk(data)
    }
    walk (data) {
        if(!data || typeof data !== 'object') return 

        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })

    }

    defineReactive(data, key, value) {
       let self = this

       //负责收集依赖，并发送通知
       let dep = new Dep()

       this.walk(value)
       Object.defineProperty(data, key, {
           enumerable: true,
           configurable: true,
           get() {
               //收集依赖   Dep.target就是观察者，在Watcher类去实现
               Dep.target && dep.addSub(Dep.target) 
               return value
           },
           set(newVal) {
               if(newVal === value) return
               value = newVal
               self.walk(newVal)

               //数据变化时发送通知
               dep.notify()
           }
       })
    }
}